function $(id) {
	return document.getElementById(id)
}

var regs = {
	userNameReg: /^(([\u4e00-\u9fa5])|[a-zA-Z0-9-_]){4,20}$/,
	pwdReg: /^.{6,20}$/,
	numReg: /\d/,
	strReg: /[a-zA-Z]/,
	tsReg: /[^\w\s]+/
}
window.onload = function() {
	var userName = $("userName");
	var pwd = $("pwd");
	var pwd2 = $("pwd2");
	var email = $("email");
	var mobile = $("mobile");
	var btn = $("btn");
	userName.onkeyup = userName.onfocus = userName.onblur = function(evt) {
		var e = evt || window.event;
		checkUsrName(e);
	}
	pwd.onkeyup = pwd.onfocus = pwd.onblur = function(evt) {
		var e = evt || window.event;
		checkPwd(e);
	}
	pwd2.onkeyup = pwd2.onfocus = pwd2.onblur = function(evt) {
		var e = evt || window.event;
		checkPwd2(e);
	}

	btn.onclick = function(evt) {
		var e = evt || window.event;
		e.preventDefault();
		checked()
		if(checkUsrName() && checkPwd() && checkPwd2() && checked()) {
			alert("可以注册");
			var url="http://jx.xuzhixiang.top/ap/api/reg.php"	
//			'user_id':token,'title':$("#title").val(),'content':$('#content').val()
			var data={'username':userName.value,'password':pwd.value}
				console.log(data)
						jQuery.ajax({
							type:"get",
							url:url,
							async:true,
							data:data,
							success:function(res){
							var obj=JSON.parse(res)
							console.log(obj)
							alert('注册成功!');
							window.location.href="../html/login.html"
							}
						});
		} else {
			return false;
		}
	}
	
	var ck=$('ck')
	ck.onclick=function(){
		checked();
		console.log(6666)
	}
}

function checked() {
	var box = ck.parentNode;
	var tip = box.nextElementSibling;
	var span = tip.lastElementChild;
	if(ck.checked) {
		console.log(5555)
		tip.className = "tip hide";
		return true;
	} else {
		tip.className = "tip error";
		span.innerHTML = "请同意协议";
		return false;
	}
}

function checkUsrName(_e) {
	var type;
	if(_e) {
		type = _e.type;
	}
	var value = userName.value;
	console.log(value)
	var box = userName.parentNode;
	console.log(box)
	var tip = box.nextElementSibling
	console.log(tip)
	var span = tip.lastElementChild;
	console.log(span)
	if(type == "focus") {
		box.className = "box";
		tip.className = "tip default";
		span.innerHTML = "支持汉字、字母、数字、“-”“_”的组合，4-20个字符";
		return false;
	}

	if(type == "blur") {
		if(value == "") {
			box.className = "box";
			tip.className = "tip hide";
		}
	}
	if(value == "") {
		box.className = "box error";
		tip.className = "tip error";
		span.innerHTML = "用户名不能为空";
		return false;
	} else if(regs.userNameReg.test(value)) {
		box.className = "box right";
		tip.className = "tip hide";
		return true;
	} else {
		box.className = "box error";
		tip.className = "tip error";
		span.innerHTML = "格式错误，仅支持汉字、字母、数字、“-”“_”的组合";
		return false;
	}
}

function checkPwd(_e) {
	var type;
	if(_e) {
		type = _e.type;
	}
	var value = pwd.value;
	var box = pwd.parentNode;
	var tip = box.nextElementSibling;
	var span = tip.lastElementChild;
	if(type == "focus") {
		box.className = "box";
		tip.className = "tip default";
		span.innerHTML = "建议使用字母、数字和符号两种以上的组合，6-20个字符";
		return false;
	}

	if(type == "blur") {
		if(value == "") {
			box.className = "box error";
			tip.className = "tip error";
			span.innerHTML = "密码不能为空";
			return false;
		}
	}
	if(value == "") {
		box.className = "box error";
		tip.className = "tip error";
		span.innerHTML = "密码不能为空";
		return false;
	} else if(regs.pwdReg.test(value)) {
		box.className = "box right";
		tip.className = "tip right";
		var level = getPwdLevel(value);
		switch(level) {
			case 1:
				tip.className = "tip ruo";
				break;
			case 2:
				tip.className = "tip zhong";
				break;
			case 3:
				tip.className = "tip qiang";
				break;
		}
		return true;
		// 弱  中  强
		// 字母、数字、特殊字符
		// level 1弱 2中  3强
		// tip ruo zhong qiang
		// jkj343?
		// tip.className = ""
	} else {
		box.className = "box error";
		tip.className = "tip error";
		span.innerHTML = "密码长度应在6-20个字符之间";
		return false;
	}
}

function getPwdLevel(pwd) {
	var level = 0;
	var numflag = true,
		strflag = true,
		tsflag = true;
	for(var i = 0; i < pwd.length; i++) {
		if(numflag && regs.numReg.test(pwd[i])) {
			level++;
			numflag = false;
			console.log(level, 111)
			continue;
		}
		if(strflag && regs.strReg.test(pwd[i])) {
			level++;
			strflag = false;
			console.log(level, 222)
			continue;
		}
		if(tsflag && regs.tsReg.test(pwd[i])) {
			level++;
			tsflag = false;
			console.log(level, 333)
		}
	}
	console.log(level)
	return level;
}

function checkPwd2(_e) {
	var type;
	if(_e) {
		type = _e.type;
	}
	var value1 = pwd.value;
	var value = pwd2.value;
	var box = pwd2.parentNode;
	var tip = box.nextElementSibling;
	var span = tip.lastElementChild;
	if(type == "focus") {
		if(value == "") {
			box.className = "box";
			tip.className = "tip default";
			span.innerHTML = "请再次输入密码";
			return false;
		}
	}
	if(value == "") {
		box.className = "box error"
		tip.className = "tip error";
		span.innerHTML = "请再次输入密码";
		return false;
	} else if(value == value1) {
		box.className = "box right";
		tip.className = "tip hide";
		return true;
	} else {
		box.className = "box error";
		tip.className = "tip error";
		span.innerHTML = "两次密码不一致";
		return false;
	}
}